package com.markby.fscm.cryptogram.domain.standard0009;

import lombok.Data;
import lombok.EqualsAndHashCode;
import org.bouncycastle.asn1.ASN1Integer;
import org.bouncycastle.asn1.ASN1Object;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.DERSequence;

import java.math.BigInteger;

/**
 * 0009规范 SM2私钥格式
 * SM2PrivateKey ::= INTEGER
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class SM2PrivateKey extends ASN1Object {

    ASN1Integer d;
    byte[] dByteArray;

    public SM2PrivateKey(BigInteger d) {
        byte[] priKeyD = d.toByteArray();
        byte[] priKey = new byte[64];

        System.arraycopy(priKeyD, priKeyD.length - 32, priKey, 32, 32);

        this.d = new ASN1Integer(d);
        this.dByteArray = priKey;
    }

    @Override
    public ASN1Primitive toASN1Primitive() {
        return new DERSequence(d);
    }
}
